import { mapGetters } from "vuex";
import { getAccountApi } from "@/api/activity";
import Routine from "@/libs/routine";
export default {
  computed: mapGetters([
    "isLogin",
    "activityIsBlack",
    "activityIsOpen",
    "activityIsNew",
    "activityNoQuery",
    "activityHadShow",
    "activityHadShowMoney",
  ]),
  data() {
    return {
      showActivity: false,
      activityType: "",
      activityMoney: "0.01",
    };
  },
  methods: {
    async getAccount() {
      const res = await getAccountApi();
      const money = +(res?.data?.balance || 0);
      if (!isNaN(money) && money > 0) {
        this.activityMoney = (money / 100).toFixed(2);
        return true;
      }
      return false;
    },
    async doActivity() {
      this.$store.commit("SET_SCROLL_LOCK", false);
      if (this.isLogin) {
        await this.$store.dispatch("GET_ACTIVITY");
        if (this.activityIsOpen && !this.activityIsBlack) {
          console.log(
            "activity. ",
            this.activityHadShow,
            this.activityHadShowMoney
          );
          // 未展示过活动
          if (!this.activityHadShow) {
            this.showActivity = true;
            this.activityType = this.activityIsNew
              ? "new"
              : this.activityNoQuery
              ? "no-query"
              : "normal";
            this.$store.commit("SET_ACTIVITY_SHOW", true);
            this.$store.commit("SET_SCROLL_LOCK", true);
            uni.hideTabBar();
          } else if (!this.activityHadShowMoney) {
            // 已展示过活动 未展示过金额
            const hasMoney = await this.getAccount();
            if (hasMoney) {
              this.showActivity = true;
              this.activityType = "reward";
              this.$store.commit("SET_ACTIVITY_M_SHOW", true);
              this.$store.commit("SET_SCROLL_LOCK", true);
            }
          }
        }
      } else if (!this.isLogin) {
        this.$store.dispatch("CLOSE_ACTIVITY");
      }
    },
  },
  async onShow() {
    uni.$on("loginSuccess", (data) => {
      this.doActivity();
    });
    // #ifdef H5
    if (!this.isLogin) {
      if (Routine.isWeixin()) {
        await this.$wxAuthorize();
      }
    }
    // #endif
    if (this.isLogin) {
      this.doActivity();
    }
  },
};
